package com.qf.edu.dao.impl;

import com.qf.edu.dao.DrugsGt;
import com.qf.edu.entity.Drugs;
import com.qf.edu.entity.Drugsgrant;
import com.qf.edu.utils.DBUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.List;

/**
 * @author:huayuzhuo
 * @email:2798866329@qq.com
 * @desc:DrugsGt接口实现类
 * @datatime: 2022/5/29 12:08
 */
public class DrugsGtImpl implements DrugsGt {
    QueryRunner queryRunner = new QueryRunner(DBUtils.getDataSource());

    @Override
    public int drugsG(Drugsgrant drugsgrant) {
        String sql = "INSERT INTO drugsgrant (`idNumber`,`name`,`drugsname`,`price`,`number`,`total`) VALUES (?,?,?,?,?,?)";
        try {
            int update = queryRunner.update(sql, new Object[]{drugsgrant.getIdNumber(), drugsgrant.getName(), drugsgrant.getDrugsname(), drugsgrant.getPrice(), drugsgrant.getNumber(), drugsgrant.getTotal()});
            return update;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public Drugs drugsGets(Drugsgrant drugsgrant) {
        String sql = "SELECT * FROM drugs WHERE `name` = ? and `price` = ?";
        try {
            Drugs query = queryRunner.query(sql, new BeanHandler<>(Drugs.class), new Object[]{drugsgrant.getDrugsname(), drugsgrant.getPrice()});
            return query;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int drugskp(int a, Drugsgrant drugsgrant) {
        String sql = "UPDATE drugs SET number = ? WHERE `name` = ? AND `price` = ?";
        try {
            int update = queryRunner.update(sql, new Object[]{a, drugsgrant.getDrugsname(),drugsgrant.getPrice()});
            return update;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

}
